import store from '@/store'

function checkPermission (el, binding) {
  // 获取传值
  const { value } = binding
  // 获取所有权限
  const points = store.getters.userInfo.permission.points
  // 当传入的指令集为数组
  if (value && value instanceof Array) {
    const currentPoint = value[0]
    const hasPermission = points.includes(currentPoint)
    // 没有权限 删除对应的功能按钮
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    // 传参有误
    throw new Error("v-permission value is ['admin', 'edit'...]")
  }
}

export default {
  // 在绑定元素的父组件被挂在后调用
  mounted (el, binding) {
    checkPermission(el, binding)
  },
  // 在包含组件的Vnode及其子组件的VNode更新后调用
  update (el, binding) {
    checkPermission(el, binding)
  }
}
